﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TitleScreenScript : MonoBehaviour {

	TextMesh highScoreText;

	Animator arrowAnim;
	SpriteRenderer arrowRend;
	Animator questionAnim;
	SpriteRenderer questionRend;
	Animator internetAnim;
	SpriteRenderer internetRend;
	Animator optionsAnim;
	SpriteRenderer optionsRend;
	Animator shareAnim;
	SpriteRenderer shareRend;
	Animator leaderAnim;
	SpriteRenderer leaderRend;
	Animator creditsAnim;
	SpriteRenderer creditsRend;
	Animator purchasesAnim;
	SpriteRenderer purchasesRend;
	Animator storyAnim;
	SpriteRenderer storyRend;

	Animator checkYesAnim;
	SpriteRenderer checkYesRend;
	Animator checkNoAnim;
	SpriteRenderer checkNoRend;

	SpriteRenderer slideRender;
	SpriteRenderer barRender;

	GameObject mainMenu;
	GameObject quitMenu;

	List<SpriteRenderer> scrollRenderers = new List<SpriteRenderer>();
	List<TextMesh> textMeshes = new List<TextMesh>();

	SpriteRenderer scrollMe1;
	TextMesh scrollMe2;

	float flickerStart = 2f;
	float flickerCount = 2f;
	bool flickState = true;

	private AdMobPlugin AndroidAdObject;

	bool inPauseMenu = false;
	float pauseSliderStart = 0.5f;
	float currentPauseSlider = 0f;

	// Use this for initialization
	void Start () {
		if (Application.platform == RuntimePlatform.IPhonePlayer) {
			Application.targetFrameRate = 60;
		}

		Time.timeScale = 1f;

		if (Application.platform == RuntimePlatform.Android) {
			this.AndroidAdObject = (AdMobPlugin)GameObject.Find("AndroidAdObject").GetComponent(typeof(AdMobPlugin));
			try {
				this.AndroidAdObject.Reconfigure();
			}
			catch {

			}
		}

		highScoreText = (TextMesh)GameObject.Find ("HighScoreText").GetComponent (typeof(TextMesh));
		GameMaintainerSingleton.Instance.GetHighScore ();
		string diff = "";
		switch(GameMaintainerSingleton.Instance.Difficulty) {
		case 0:diff = "Easy";break;
		case 1:diff = "Normal";break;
		case 2:diff = "Hard";break;
		default:break;
		}
		highScoreText.text = "High Score ("+diff+"): " + GameMaintainerSingleton.Instance.HighScore;

		this.questionAnim = (Animator)GameObject.Find ("Question").GetComponent (typeof(Animator));
		this.arrowAnim = (Animator)GameObject.Find ("Arrow").GetComponent (typeof(Animator));
		this.questionRend = (SpriteRenderer)GameObject.Find("Question").GetComponent (typeof(SpriteRenderer));
		this.arrowRend = (SpriteRenderer)GameObject.Find("Arrow").GetComponent (typeof(SpriteRenderer));
		this.internetAnim = (Animator)GameObject.Find ("Internets").GetComponent (typeof(Animator));
		this.internetRend = (SpriteRenderer)GameObject.Find ("Internets").GetComponent (typeof(SpriteRenderer));
		this.optionsAnim = (Animator)GameObject.Find ("Options").GetComponent (typeof(Animator));
		this.optionsRend = (SpriteRenderer)GameObject.Find ("Options").GetComponent (typeof(SpriteRenderer));
		this.shareAnim = (Animator)GameObject.Find ("Share").GetComponent (typeof(Animator));
		this.shareRend = (SpriteRenderer)GameObject.Find ("Share").GetComponent (typeof(SpriteRenderer));
		this.leaderAnim = (Animator)GameObject.Find ("Leaderboards").GetComponent (typeof(Animator));
		this.leaderRend = (SpriteRenderer)GameObject.Find ("Leaderboards").GetComponent (typeof(SpriteRenderer));
		this.creditsAnim = (Animator)GameObject.Find ("Credits").GetComponent (typeof(Animator));
		this.creditsRend = (SpriteRenderer)GameObject.Find ("Credits").GetComponent (typeof(SpriteRenderer));

		this.checkYesAnim = (Animator)GameObject.Find ("CheckYes").GetComponent (typeof(Animator));
		this.checkYesRend = (SpriteRenderer)GameObject.Find ("CheckYes").GetComponent (typeof(SpriteRenderer));
		this.checkNoAnim = (Animator)GameObject.Find ("CheckNo").GetComponent (typeof(Animator));
		this.checkNoRend = (SpriteRenderer)GameObject.Find ("CheckNo").GetComponent (typeof(SpriteRenderer));

		this.quitMenu = (GameObject)GameObject.Find("QuitMenu");


		//this.purchasesAnim = (Animator)GameObject.Find ("Purchases").GetComponent (typeof(Animator));
		//this.purchasesRend = (SpriteRenderer)GameObject.Find ("Purchases").GetComponent (typeof(SpriteRenderer));

		this.storyAnim = (Animator)GameObject.Find ("Story").GetComponent (typeof(Animator));
		this.storyRend = (SpriteRenderer)GameObject.Find ("Story").GetComponent (typeof(SpriteRenderer));


		this.slideRender = (SpriteRenderer)GameObject.Find("ScrollMarker").GetComponent(typeof(SpriteRenderer));
		this.barRender = (SpriteRenderer)GameObject.Find("ScrollBar").GetComponent(typeof(SpriteRenderer));

		this.mainMenu = (GameObject)GameObject.Find("MainMenu");

		this.arrowAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.ArrowUp);
		this.questionAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.QuestionUp);
		this.internetAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.InternetUp);
		this.optionsAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.OptionsUp);
		this.shareAnim.SetInteger (MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.ShareUp);
		this.leaderAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.LeaderboardUp);
		this.creditsAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.CreditsUp);
		//this.purchasesAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.PurchasesUp);
		this.storyAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.StoryUp);

		this.checkNoAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.CheckNoUp);
		this.checkYesAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.CheckYesUp);

		int children = this.mainMenu.transform.childCount;
		for (int z=0;z<children;z++) {
			Transform child = this.mainMenu.transform.GetChild(z);
			if (child.name != "ScrollMe" && child.name != "ScrollText") {
				SpriteRenderer rend = (SpriteRenderer)child.gameObject.GetComponent(typeof(SpriteRenderer));
				TextMesh textMesh = (TextMesh)child.GetChild(0).gameObject.GetComponent(typeof(TextMesh));
				this.scrollRenderers.Add(rend);
				this.textMeshes.Add(textMesh);
			}
		}

		this.scrollMe1 = (SpriteRenderer)GameObject.Find("ScrollMe").GetComponent(typeof(SpriteRenderer));
		this.scrollMe2 = (TextMesh)GameObject.Find("ScrollText").GetComponent(typeof(TextMesh));

		this.UpdateBarFade();

		GameMaintainerSingleton.Instance.ShowAds ();
	}

	void UpdateBarFade() {
		Vector3 minBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.min);
		Vector3 maxBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.max);
		
		
		float barMin = minBounds.x + 100f;
		float barMax = maxBounds.x - 100f;
		
		for (int z=0;z<scrollRenderers.Count;z++) {
			SpriteRenderer rend = this.scrollRenderers[z];
			TextMesh rendText = this.textMeshes[z];
			Vector3 rendMin = Camera.main.WorldToScreenPoint(rend.bounds.min);
			Vector3 rendMax = Camera.main.WorldToScreenPoint(rend.bounds.max);
			Color col = rend.color;
			Vector3 scaleRend = rend.transform.localScale;
			if (rendMin.x > barMax) {
				float a = Mathf.Lerp(1f,0f,(rendMin.x - barMax) / ((float)Screen.width - barMax));
				float sc = Mathf.Lerp(6f,12f,(rendMin.x - barMax) / ((float)Screen.width - barMax));
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else if (rendMax.x < barMin) {
				float a = Mathf.Lerp(0f,1f,rendMax.x / barMin);
				float sc = Mathf.Lerp(12f,6f,rendMax.x / barMin);
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else {
				float a = 1f;
				float sc = 6f;
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			//Debug.Log (child.name + "/" + barMin + "/" + barMax + "/" + col.ToString());
			rendText.color = col;
			rend.color = col;
			rend.transform.localScale = scaleRend;
		}

	}

	void UpdateScrollText() {
		flickerCount -= Time.deltaTime;
		if (flickerCount < 0f) {
			flickerCount = flickerStart;
			flickState = !flickState;
			Color col = this.scrollMe1.color;
			if (flickState) {
				col.a = 1f;
			}
			else {
				col.a = 0.25f;
			}
			this.scrollMe1.color = col;
			this.scrollMe2.color = col;
		}
	}
	
	// Update is called once per frame
	void Update () {
		//Vector2 pos = Vector2.zero;
		this.UpdateScrollText();

		bool inArrow = false; bool inArrowInitially = false;
		bool inQuestion = false; bool inQuestionInitially = false;
		bool inInternet = false; bool inInternetInitially = false;
		bool inOptions = false; bool inOptionsInitially = false;
		bool inShare = false; bool inShareInitially = false;
		bool inLeader = false; bool inLeaderInitially = false;
		bool inCredits = false; bool inCreditsInitially = false;
		bool inPurchases = false; bool inPurchasesInitially = false;
		bool inStory = false; bool inStoryInitially = false;

		bool inScrollBar = false;
		bool inScroll = false;

		bool inCheckYes = false; bool inCheckYesInitially = false;
		bool inCheckNo = false; bool inCheckNoInitially = false;

		if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {
			inArrow = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.arrowRend, GameMaintainerSingleton.Instance.Pos);
			inQuestion = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.questionRend, GameMaintainerSingleton.Instance.Pos);
			inInternet = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.internetRend, GameMaintainerSingleton.Instance.Pos);
			inOptions = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.optionsRend, GameMaintainerSingleton.Instance.Pos);
			inShare = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.shareRend, GameMaintainerSingleton.Instance.Pos);
			inLeader = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.leaderRend, GameMaintainerSingleton.Instance.Pos);
			inCredits = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.creditsRend, GameMaintainerSingleton.Instance.Pos);
			//inPurchases = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.purchasesRend, GameMaintainerSingleton.Instance.Pos);
			inStory = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.storyRend, GameMaintainerSingleton.Instance.Pos);

			inCheckNo = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkNoRend, GameMaintainerSingleton.Instance.Pos);
			inCheckYes = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkYesRend, GameMaintainerSingleton.Instance.Pos);

			inArrowInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.arrowRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inQuestionInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.questionRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inInternetInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.internetRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inOptionsInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.optionsRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inShareInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.shareRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inLeaderInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.leaderRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inCreditsInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.creditsRend, GameMaintainerSingleton.Instance.ClickDownPos);
			//inPurchasesInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.purchasesRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inStoryInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.storyRend, GameMaintainerSingleton.Instance.ClickDownPos);

			inCheckNoInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkNoRend, GameMaintainerSingleton.Instance.ClickDownPos);
			inCheckYesInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkYesRend, GameMaintainerSingleton.Instance.ClickDownPos);


			inScroll = GameMaintainerSingleton.Instance.IsInSpriteY(this.slideRender, GameMaintainerSingleton.Instance.ClickDownPos,10);
			inScrollBar = GameMaintainerSingleton.Instance.IsInSpriteY(this.barRender, GameMaintainerSingleton.Instance.Pos,10);

			GameMaintainerSingleton.SpriteBoundsSplitViewModel vm = new GameMaintainerSingleton.SpriteBoundsSplitViewModel();
			if (!inPauseMenu) {
				if (inScroll) {
					vm = GameMaintainerSingleton.Instance.CheckBarNoSplit(
						inScroll,inScrollBar,slideRender,barRender,
						GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 40); 

					//Min 4.75
					//Max -17.41418
					Vector3 mainMenuPos = this.mainMenu.transform.position;
					mainMenuPos.x = Mathf.Lerp(4.64f,-17.41418f,vm.RelativeDistance);
					this.mainMenu.transform.position = mainMenuPos;

					this.UpdateBarFade();
				}
			}
		}

		if (Input.GetKeyUp(KeyCode.Escape)) {
			this.inPauseMenu = !this.inPauseMenu;
			if (this.currentPauseSlider > 0f) {
				this.currentPauseSlider = this.pauseSliderStart - this.currentPauseSlider;
			}
			else { 
				this.currentPauseSlider = this.pauseSliderStart;
			}
		}

		if (currentPauseSlider > 0.0f) {
			currentPauseSlider -= Time.deltaTime;
			
			Vector3 pos = this.quitMenu.transform.position;
			float posMinY = 1.398989f;
			float posMaxY = 9.60141f;
			float currentPos = pos.y;
			float newPosY = 0f;
			if (this.inPauseMenu) {
				newPosY = Mathf.Lerp(posMaxY,posMinY,1.0f - currentPauseSlider * 2f);
			}
			else {
				newPosY = Mathf.Lerp(posMinY,posMaxY,1.0f - currentPauseSlider * 2f);
			}

			pos.y = newPosY;
			this.quitMenu.transform.position = pos;
		}

		if (inPauseMenu) {
			// Yes Button
			if (GameMaintainerSingleton.Instance.CheckButton(
				inCheckYes,inCheckYesInitially,
				checkYesRend,checkYesAnim,
				MenuButtonConstants.MenuConstants.CheckYesDown,MenuButtonConstants.MenuConstants.CheckYesUp,
				MenuButtonConstants.MenuTriggerString)) {
				Application.Quit();
			}
			// No Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inCheckNo,inCheckNoInitially,
				checkNoRend,checkNoAnim,
				MenuButtonConstants.MenuConstants.CheckNoDown,MenuButtonConstants.MenuConstants.CheckNoUp,
				MenuButtonConstants.MenuTriggerString)) {
				this.inPauseMenu = false;
				if (this.currentPauseSlider > 0f) {
					this.currentPauseSlider = this.pauseSliderStart - this.currentPauseSlider;
				}
				else { 
					this.currentPauseSlider = this.pauseSliderStart;
				}
			}
		}
		else if (!inPauseMenu) {
			// Play Button
			if (GameMaintainerSingleton.Instance.CheckButton(
				inArrow,inArrowInitially,
				arrowRend,arrowAnim,
				MenuButtonConstants.MenuConstants.ArrowDown,MenuButtonConstants.MenuConstants.ArrowUp,
				MenuButtonConstants.MenuTriggerString)) {
				if (GameMaintainerSingleton.Instance.FirstTimeUsage) {
					// Load Tutorial
					GameMaintainerSingleton.Instance.CloseAds();
					Application.LoadLevel(2);
				}
				else {
					// Load Game
					GameMaintainerSingleton.Instance.CloseAds();
					Application.LoadLevel(1);
				}
			}
			// Help Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inQuestion,inQuestionInitially,
				questionRend,questionAnim,
				MenuButtonConstants.MenuConstants.QuestionDown,MenuButtonConstants.MenuConstants.QuestionUp,
				MenuButtonConstants.MenuTriggerString)) {
				// Load Tutorial
				GameMaintainerSingleton.Instance.CloseAds();
				Application.LoadLevel(2);
			}
			// Website Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inInternet,inInternetInitially,
				internetRend,internetAnim,
				MenuButtonConstants.MenuConstants.InternetDown,MenuButtonConstants.MenuConstants.InternetUp,
				MenuButtonConstants.MenuTriggerString)) {
				Application.OpenURL("https://www.johnrussellernest.com");
			}
			// Options Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inOptions,inOptionsInitially,
				optionsRend,optionsAnim,
				MenuButtonConstants.MenuConstants.OptionsDown,MenuButtonConstants.MenuConstants.OptionsUp,
				MenuButtonConstants.MenuTriggerString)) {
				// Load Options
				GameMaintainerSingleton.Instance.CloseAds();
				Application.LoadLevel(4);
			}
			// Share Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inShare, inShareInitially, 
				shareRend, shareAnim, 
				MenuButtonConstants.MenuConstants.ShareDown, MenuButtonConstants.MenuConstants.ShareUp,
				MenuButtonConstants.MenuTriggerString)) {
				Application.LoadLevel(5);
			}
			// Leaderboards Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inLeader, inLeaderInitially, 
				leaderRend, leaderAnim, 
				MenuButtonConstants.MenuConstants.LeaderboardDown, MenuButtonConstants.MenuConstants.LeaderboardUp,
				MenuButtonConstants.MenuTriggerString)) {

				if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
					if (!Social.localUser.authenticated) {
						Social.localUser.Authenticate((bool success) => {
							if (success) {
								Social.ShowLeaderboardUI();
							}
						});
					}
					else {
						Social.ShowLeaderboardUI();
					}
				}
				else if (Application.platform == RuntimePlatform.WP8Player) {
					try {
						PubCenterPlugin.PubCenterInteraction.ShowLeaderboard();
					}
					catch(System.Exception ex) {
						this.highScoreText.text = ex.ToString();
					}
				}
			}
			// Credits Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inCredits, inCreditsInitially, 
				creditsRend, creditsAnim, 
				MenuButtonConstants.MenuConstants.CreditsDown, MenuButtonConstants.MenuConstants.CreditsUp,
				MenuButtonConstants.MenuTriggerString)) {

				GameMaintainerSingleton.Instance.CloseAds();
				Application.LoadLevel(6);
			}
			// Story Button
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inStory, inStoryInitially, 
				storyRend, storyAnim, 
				MenuButtonConstants.MenuConstants.StoryDown, MenuButtonConstants.MenuConstants.StoryUp,
				MenuButtonConstants.MenuTriggerString)) {
				GameMaintainerSingleton.Instance.CloseAds();
				Application.LoadLevel(7);
			}


			// Purchases Button
			/*
			else if (GameMaintainerSingleton.Instance.CheckButton(
				inPurchases, inPurchasesInitially, 
				purchasesRend, purchasesAnim, 
				MenuButtonConstants.MenuConstants.PurchasesDown, MenuButtonConstants.MenuConstants.PurchasesUp,
				MenuButtonConstants.MenuTriggerString)) {
				if (Application.platform == RuntimePlatform.WP8Player) {
					PubCenterPlugin.PubCenterInteraction.ShowInAppPurchases();
				}
			}
			*/
		}

	}
}
